﻿using System;
using System.Collections.Generic;
using System.Xml;
using Assist;
using Autodesk.Revit.DB;
using YArchitech.Revit;
using YJKOpeningCommon.DataEnum;
using YJKOpeningCommon.OpeningEX;
using YJKOpeningCommon.OpeningProfile;
using YJKOpeningCommon.Public;
using YJKOpeningCreate.Public;

namespace YJKOpeningCommon.DbOpening
{
	public class DbRectWallOpening : DbFamilyOpening
	{
		public override bool CreateOpeningFamilyInstance(ViewScene ViewScene, out Element elem3D, out Element elem2D)
		{
			elem2D = null;
			elem3D = null;
			Autodesk.Revit.DB.View viewOpening2D = ViewScene.m_viewOpening2D;
			if (viewOpening2D == null)
			{
				elem3D = null;
				return false;
			}
			Document document = viewOpening2D.Document;
			OpeningXData openingXData = new OpeningXData();
			base.GetOpeningXDataBase(ref openingXData);
			Transaction transaction = new Transaction(document, "CreateOpeningFamilyInstance");
			try
			{
				FamilySymbolOperator familySymbolOperator = new FamilySymbolOperator(document, this);
				if (!familySymbolOperator.DoCalculate())
				{
					return false;
				}
				FamilySymbol familySymble = familySymbolOperator.GetFamilySymble();
				transaction.Start();
				OpeningProfileRect openingProfileRect = this.m_profile as OpeningProfileRect;
				Wall wall = document.GetElement(this.m_strHostUniqueId) as Wall;
				Level level = document.GetElement(wall.LevelId) as Level;
				UnifiedModified.ActivateFamilySymbol(familySymble);
				FamilyInstance familyInstance = document.Create.NewFamilyInstance(openingProfileRect.m_position, familySymble, wall, level, (Autodesk.Revit.DB.Structure.StructuralType)0);
				double num = 0.0;
				double num2 = 0.0;
				if (familySymbolOperator.IsNeedSetSize())
				{
					num = openingProfileRect.m_dExtendDist * 2.0 + openingProfileRect.m_cutHeight;
					num2 = openingProfileRect.m_dExtendDist * 2.0 + openingProfileRect.m_cutWidth;
					familyInstance.GetParameter("洞口高度").Set(num);
					familyInstance.GetParameter("洞口宽度").Set(num2);
				}
				openingXData.m_eOpeningProfile = 1;
				openingXData.m_dHeight = num;
				openingXData.m_dWidth = num2;
				openingXData.m_dMaxZ = openingProfileRect.m_position.Z + num / 2.0;
				openingXData.m_dMinZ = openingProfileRect.m_position.Z - num / 2.0;
				openingXData.WriteOpeningXData(familyInstance);
				transaction.Commit();
				elem3D = familyInstance;
			}
			catch (Exception)
			{
				elem3D = null;
				transaction.RollBack();
				return false;
			}
			return true;
		}

		public override bool CreateOpeningPreviewInstance(out Element elemPreview, PreViewScene preViewScene)
		{
			OpeningProfileRect openingProfileRect = this.m_profile as OpeningProfileRect;
			XYZ position = openingProfileRect.m_position;
			XYZ directionProfile = openingProfileRect.m_directionProfile;
			Transaction transaction = new Transaction(preViewScene.m_doc, "CreateModelGroup");
			bool result;
			try
			{
				XYZ xyz = XYZ.BasisZ.CrossProduct(directionProfile);
				XYZ xyz2 = position + directionProfile * (this.m_dHostWidth / 2.0 + preViewScene.m_offset) + XYZ.BasisZ.Negate() * (openingProfileRect.m_cutHeight / 2.0 + openingProfileRect.m_dExtendDist);
				XYZ xyz3 = xyz2 + (openingProfileRect.m_dExtendDist + openingProfileRect.m_cutWidth / 2.0) * xyz;
				XYZ xyz4 = xyz2 + (openingProfileRect.m_dExtendDist + openingProfileRect.m_cutWidth / 2.0) * xyz.Negate();
				XYZ item = xyz4 + (openingProfileRect.m_dExtendDist * 2.0 + openingProfileRect.m_cutHeight) * XYZ.BasisZ;
				XYZ item2 = xyz3 + (openingProfileRect.m_dExtendDist * 2.0 + openingProfileRect.m_cutHeight) * XYZ.BasisZ;
				List<ElementId> list = new List<ElementId>();
				List<XYZ> list2 = new List<XYZ>();
				list2.Add(xyz3);
				list2.Add(xyz4);
				list2.Add(item);
				list2.Add(item2);
				SketchPlane sketchPlane = null;
				if (!CommonInterface.CreateSketchPlane(preViewScene.m_doc, openingProfileRect.m_directionProfile, xyz3, ref sketchPlane) || sketchPlane == null)
				{
					elemPreview = null;
					result = false;
				}
				else if (!CommonInterface.CreateModelLine(list2, preViewScene.m_doc, sketchPlane, ref list, preViewScene.m_modelLineElemId))
				{
					elemPreview = null;
					result = false;
				}
				else
				{
					XYZ xyz5 = position + directionProfile.Negate() * (this.m_dHostWidth / 2.0 + preViewScene.m_offset) + XYZ.BasisZ.Negate() * (openingProfileRect.m_cutHeight / 2.0 + openingProfileRect.m_dExtendDist);
					XYZ xyz6 = xyz5 + (openingProfileRect.m_dExtendDist + openingProfileRect.m_cutWidth / 2.0) * xyz;
					XYZ xyz7 = xyz5 + (openingProfileRect.m_dExtendDist + openingProfileRect.m_cutWidth / 2.0) * xyz.Negate();
					XYZ item3 = xyz7 + (openingProfileRect.m_dExtendDist * 2.0 + openingProfileRect.m_cutHeight) * XYZ.BasisZ;
					XYZ item4 = xyz6 + (openingProfileRect.m_dExtendDist * 2.0 + openingProfileRect.m_cutHeight) * XYZ.BasisZ;
					list2.Clear();
					list2.Add(xyz6);
					list2.Add(xyz7);
					list2.Add(item3);
					list2.Add(item4);
					sketchPlane = null;
					if (!CommonInterface.CreateSketchPlane(preViewScene.m_doc, openingProfileRect.m_directionProfile, xyz6, ref sketchPlane))
					{
						elemPreview = null;
						result = false;
					}
					else if (!CommonInterface.CreateModelLine(list2, preViewScene.m_doc, sketchPlane, ref list, preViewScene.m_modelLineElemId))
					{
						elemPreview = null;
						result = false;
					}
					else
					{
						transaction.Start();
						elemPreview = preViewScene.m_doc.Create.NewGroup(list);
						transaction.Commit();
						result = true;
					}
				}
			}
			catch (Exception)
			{
				transaction.RollBack();
				elemPreview = null;
				result = false;
			}
			return result;
		}

		public override bool LoadOpeningFamily(Autodesk.Revit.DB.Document doc, ref FamilySymbol familySymbol)
		{
			base.SetFamilyName("墙洞口-矩形");
			return base.LoadOpeningFamily(doc, ref familySymbol);
		}

		public override bool IsNeedCreateFamily()
		{
			return true;
		}

		public override bool Equals(object obj)
		{
			return base.Equals(obj);
		}

		public override int GetHashCode()
		{
			return base.GetHashCode();
		}

		public override void AddSharedParameters(Element elem)
		{
			Transaction transaction = new Transaction(elem.Document, "AddSharedParameter");
			try
			{
				transaction.Start();
				base.AddSharedParameters(elem);
				Parameter parameter = elem.GetParameter(DbOpeningBase.m_strSharedParaOpeningMajor);
				if (parameter != null && StrSpecialityObj.m_strSpecialityObj.GetLength(0) > (int)this.m_eSpeciality)
				{
					parameter.Set(StrSpecialityObj.m_strSpecialityObj[(int)this.m_eSpeciality]);
				}
				parameter = elem.GetParameter(DbOpeningBase.m_strSharedParaOpeningSize);
				if (parameter != null)
				{
					string openingSize = this.GetOpeningSize(false);
					parameter.Set(openingSize);
				}
				parameter = elem.GetParameter(DbOpeningBase.m_strSharedParaOpeningElevTop);
				if (parameter != null)
				{
					string dimLabelItem = this.GetDimLabelItem(EDimItemType.eTop);
					parameter.Set(dimLabelItem);
				}
				parameter = elem.GetParameter(DbOpeningBase.m_strSharedParaOpeningElevCenter);
				if (parameter != null)
				{
					string dimLabelItem2 = this.GetDimLabelItem(EDimItemType.eMid);
					parameter.Set(dimLabelItem2);
				}
				parameter = elem.GetParameter(DbOpeningBase.m_strSharedParaOpeningElevBottom);
				if (parameter != null)
				{
					string dimLabelItem3 = this.GetDimLabelItem(EDimItemType.eBottom);
					parameter.Set(dimLabelItem3);
				}
				parameter = elem.GetParameter(DbOpeningBase.m_strSharedParaOpeningElev);
				if (parameter != null)
				{
					parameter.Set("");
				}
				transaction.Commit();
			}
			catch
			{
				transaction.RollBack();
			}
		}

		public override bool Export2XmlNode(XmlElement xmlNode)
		{
			bool result;
			try
			{
				if (!base.Export2XmlNode(xmlNode))
				{
					result = false;
				}
				else
				{
					result = true;
				}
			}
			catch
			{
				xmlNode = null;
				result = false;
			}
			return result;
		}

		public override bool ImportByXmlNode(XmlNode xmlNode)
		{
			try
			{
				if (!base.ImportByXmlNode(xmlNode))
				{
					return false;
				}
			}
			catch
			{
				return false;
			}
			return true;
		}

		public override string GetDimLabelItem(EDimItemType eDimItemTyppe)
		{
			OpeningProfileRect openingProfileRect = this.m_profile as OpeningProfileRect;
			if (openingProfileRect == null)
			{
				return null;
			}
			double num = openingProfileRect.m_cutHeight / 2.0 + openingProfileRect.m_dExtendDist;
			string result = string.Empty;
			double num2 = AssistFunc.feetToMM(openingProfileRect.m_position.Z) / 1000.0;
			double num3 = num2 + AssistFunc.feetToMM(num / 1000.0);
			double num4 = num2 - AssistFunc.feetToMM(num / 1000.0);
			base.TransformElev2Relative(ref num2, ref num3, ref num4);
			switch (eDimItemTyppe)
			{
			case EDimItemType.eBottom:
				result = "底部标高" + num4.ToString("f3");
				break;
			case EDimItemType.eMid:
				result = "中心标高" + num2.ToString("f3");
				break;
			case EDimItemType.eTop:
				result = "顶部标高" + num3.ToString("f3");
				break;
			}
			return result;
		}

		public override void GetProfileOutLine3D(ref List<XYZ> lstOutLineView3DPt, ref Transform transform, bool isExtend, XYZ location = null)
		{
			List<XYZ> list = new List<XYZ>();
			this.m_profile.GetProfileOutLineOnXOY(ref list, isExtend);
			if (transform == null)
			{
				transform = Transform.CreateRotation(XYZ.BasisZ, 0.0);
				transform.BasisY = XYZ.BasisZ;
				transform.BasisZ = this.m_profile.m_directionProfile.Normalize();
				transform.BasisX = transform.BasisY.CrossProduct(transform.BasisZ).Normalize();
				transform.Origin = this.m_profile.m_position;
			}
			else if (location != null)
			{
				transform.Origin = location;
			}
			foreach (XYZ xyz in list)
			{
				lstOutLineView3DPt.Add(transform.OfPoint(xyz));
			}
		}
	}
}
